<style>
	.layui-btn+.layui-btn {margin-left: 0;}
	.layui-btn {margin-right: 10px; margin-bottom: 15px;}
	.weight-list span{display: inline-block;width: 60px;line-height: 30px;border:1px solid #eee;border-radius: 4px;text-align: center;font-size: 12px;cursor: pointer;}
	.weight-list span.disabled{background:#f1f1f1;cursor: not-allowed; }
	.layui-btn.layui-btn-primary.level-btn span{margin-left: 5px}
</style>

<div class="layui-form form-wrap">
    <!-- 基础上传 -->
    <div class="layui-card card-common card-brief">
		<div class="layui-card-header">
		    <span class="card-title">等级佣金比例</span>
		</div>	
		
		<div class="layui-card-body">
			{if $info.is_default eq 0}
			<div class="layui-form-item">
				<label class="layui-form-label"><span class="required">*</span>等级权重：</label>
				<div class="layui-input-block weight-list">
					<span value="1" class="{if $info.level_num eq 1}border-color{/if} {if in_array(1, $level_weight)}disabled{/if}">一级</span>
					<span value="2" class="{if $info.level_num eq 2}border-color{/if} {if in_array(2, $level_weight)}disabled{/if}">二级</span>
					<!-- <span value="3" class="{if $info.level_num eq 3}border-color{/if} {if in_array(3, $level_weight)}disabled{/if}">三级</span>
					<span value="4" class="{if $info.level_num eq 4}border-color{/if} {if in_array(4, $level_weight)}disabled{/if}">四级</span>
					<span value="5" class="{if $info.level_num eq 5}border-color{/if} {if in_array(5, $level_weight)}disabled{/if}">五级</span>
					<span value="6" class="{if $info.level_num eq 6}border-color{/if} {if in_array(6, $level_weight)}disabled{/if}">六级</span>
					<span value="7" class="{if $info.level_num eq 7}border-color{/if} {if in_array(7, $level_weight)}disabled{/if}">七级</span>
					<span value="8" class="{if $info.level_num eq 8}border-color{/if} {if in_array(8, $level_weight)}disabled{/if}">八级</span>
					<span value="9" class="{if $info.level_num eq 9}border-color{/if} {if in_array(9, $level_weight)}disabled{/if}">九级</span>
					<span value="10" class="{if $info.level_num eq 10}border-color{/if} {if in_array(10, $level_weight)}disabled{/if}">十级</span> -->
				</div>
				<div class="word-aux">
					<p>等级权重越大等级越高</p>
				</div>
			</div>
			{/if}

			<div class="layui-form-item">
				<label class="layui-form-label"><span class="required">*</span>等级名称：</label>
				<div class="layui-input-block">
					<input type="text" name="level_name" value="{$info.level_name}" lay-verify="required"  autocomplete="off" class="layui-input len-mid" maxlength="40" placeholder="请输入等级名称" title="等级名称">
				</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label"><span class="required">*</span>直推奖：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" name="recommend_rate" value="{$info.recommend_rate}" lay-verify="required|money" autocomplete="off" class="layui-input len-mid" placeholder="请输入直推奖比例" title="直推奖比例">
				</div>
				<div class="layui-form-mid">% 或</div>
				<div class="layui-input-inline">
					<input type="number" min="0" name="recommend_money" value="{$info.recommend_money}" autocomplete="off" class="layui-input len-mid" placeholder="请输入直推奖金额" title="直推奖金额">
				</div>
				<div class="layui-form-mid">元</div>
				<div class="word-aux">
					<p>会员购买后给直推人的奖励比例或固定金额</p>
				</div>
			</div>

			<div class="layui-form-item seen-point-reward" style="display: none;">
				<label class="layui-form-label"><span class="required">*</span>见点奖：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" name="see_rate" value="{$info.see_rate}" lay-verify="required|money" autocomplete="off" class="layui-input len-mid" placeholder="请输入见点奖比例" title="见点奖比例">
				</div>
				<div class="layui-form-mid">% 或</div>
				<div class="layui-input-inline">
					<input type="number" min="0" name="see_money" value="{$info.see_money}" autocomplete="off" class="layui-input len-mid" placeholder="请输入见点奖金额" title="见点奖金额">
				</div>
				<div class="layui-form-mid">元</div>
				<div class="word-aux">
					<p>会员购买后给见点人的奖励比例或固定金额</p>
				</div>
			</div>

			<!-- <div class="layui-form-item gratitude-reward" style="display: none;">
				<label class="layui-form-label"><span class="required">*</span>感恩奖：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" name="cultivate_rate" value="{$info.cultivate_rate}" lay-verify="required|money"  autocomplete="off" class="layui-input len-mid" placeholder="请输入感恩奖比例" title="感恩奖比例">
				</div>
				<div class="layui-form-mid">% 或</div>
				<div class="layui-input-inline">
					<input type="number" min="0" name="cultivate_money" value="{$info.cultivate_money}" autocomplete="off" class="layui-input len-mid" placeholder="请输入感恩奖金额" title="感恩奖金额">
				</div>
				<div class="layui-form-mid">元</div>
				<div class="word-aux">
					<p>会员购买后给培育人的奖励比例或固定金额</p>
				</div>
			</div> -->
			
			<!-- 平级奖设置 -->
			<div class="layui-form-item peer-level-reward" style="display: none;">
				<label class="layui-form-label"><span class="required">*</span>平级奖：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" name="equal_rate" value="{$info.equal_rate}" lay-verify="required|money"  autocomplete="off" class="layui-input len-mid" placeholder="请输入平级奖比例" title="平级奖比例">
				</div>
				<div class="layui-form-mid">% 或</div>
				<div class="layui-input-inline">
					<input type="number" min="0" name="equal_money" value="{$info.equal_money}" autocomplete="off" class="layui-input len-mid" placeholder="请输入平级奖金额" title="平级奖金额">
				</div>
				<div class="layui-form-mid">元</div>
				<div class="word-aux">
					<p>会员购买后给平级人的奖励比例或固定金额</p>
				</div>
			</div>

			<!-- 复购见单奖设置 -->
			<div class="layui-form-item fusee-reward" style="display: none;">
				<label class="layui-form-label"><span class="required">*</span>复购见单奖：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" name="fusee_rate" value="{$info.fusee_rate}" lay-verify="required|money"  autocomplete="off" class="layui-input len-mid" placeholder="请输入复购见单奖比例" title="复购见单奖比例">
				</div>
				<div class="layui-form-mid">% 或</div>
				<div class="layui-input-inline">
					<input type="number" min="0" name="fusee_money" value="{$info.fusee_money}" autocomplete="off" class="layui-input len-mid" placeholder="请输入复购见单奖金额" title="复购见单奖金额">
				</div>
				<div class="layui-form-mid">元</div>
				<div class="word-aux">
					<p>会员购买后给复购见单人的奖励比例或固定金额</p>
				</div>
			</div>

			
    </div>
</div>

<!-- 冻结设置 -->
<div class="layui-card card-common card-brief frozen-settings">
	<div class="layui-card-header">
		<span class="card-title">冻结设置</span>
	</div>
	<div class="layui-card-body">
		<div class="layui-form-item">
			<label class="layui-form-label">见点奖冻结：</label>
			<div class="layui-input-block">
				<div class="layui-input-inline">
					<input type="radio" name="see_frozen" value="1" title="开启" {if $info.see_frozen == 1} checked {/if}/>
					<input type="radio" name="see_frozen" value="0" title="关闭" {if $info.see_frozen != 1} checked {/if}/>
				</div>
			</div>
			<div class="word-aux">开启后，见点奖将被冻结，需要完成指定任务后才能解冻</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">感恩奖冻结：</label>
			<div class="layui-input-block">
				<div class="layui-input-inline">
					<input type="radio" name="cultivate_frozen" value="1" title="开启" {if $info.cultivate_frozen == 1} checked {/if}/>
					<input type="radio" name="cultivate_frozen" value="0" title="关闭" {if $info.cultivate_frozen != 1} checked {/if}/>
				</div>
			</div>
			<div class="word-aux">开启后，感恩奖将被冻结，需要完成指定任务后才能解冻</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">冻结比例：</label>
			<div class="layui-input-inline">
				<input type="number" min="0" max="100" name="frozen_rate" value="{$info.frozen_rate}" autocomplete="off" class="layui-input len-short" placeholder="请输入冻结比例" title="冻结比例">
			</div>
			<div class="layui-form-mid">%</div>
			<div class="word-aux">设置奖励的冻结比例，0表示不冻结，100表示全部冻结</div>
		</div>
	</div>
</div>

<!-- 解冻设置 -->
<div class="layui-card card-common card-brief unfrozen-settings">
	<div class="layui-card-header">
		<span class="card-title">解冻任务设置</span>
	</div>
	<div class="layui-card-body">
		
		<div class="layui-form-item">
			<label class="layui-form-label">直推前二代理：</label>
			<div class="layui-input-block">
				<div class="layui-input-inline">
					<input type="radio" name="task_boss_first" value="1" title="开启" {if $info.task_boss_first == 1} checked {/if}/>
					<input type="radio" name="task_boss_first" value="0" title="关闭" {if $info.task_boss_first != 1} checked {/if}/>
				</div>
			</div>
			<div class="word-aux">开启后，只计算前两个直推代理升级为组长的情况</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">直推代理升级任务：</label>
			<div class="layui-input-inline">
				<input type="number" min="0" name="task_boss_num" value="{$info.task_boss_num}" autocomplete="off" class="layui-input len-short" placeholder="请输入人数" title="直推代理升级为组长数量">
			</div>
			<div class="layui-form-mid">人</div>
			<div class="word-aux">直推代理升级为组长的人数要求，0表示不限制</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">任务有效天数：</label>
			<div class="layui-input-inline">
				<input type="number" min="0" name="task_limit_days" value="{$info.task_limit_days}" autocomplete="off" class="layui-input len-short" placeholder="请输入天数" title="任务有效天数">
			</div>
			<div class="layui-form-mid">天</div>
			<div class="word-aux">设置任务有效期，0表示不限制时间，超时则冻结奖励失效</div>
		</div>
	</div>
</div>

    {if $info.is_default eq 0}
    <div class="layui-card card-common card-brief">
        <div class="layui-card-header">
            <span class="card-title">升级条件</span>
        </div>
		
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label class="layui-form-label">升级方式：</label>
                <div class="layui-input-block">
					<input type="radio" name="upgrade_type" value="1" lay-filter="withdraw_type" title="满足以下任意条件" checked />
					<input type="radio" name="upgrade_type" value="2" lay-filter="withdraw_type" {if $info.upgrade_type == 2} checked {/if} title="满足以下全部条件" />
                </div>
            </div>
			
			<div class="layui-form-item">
			    <label class="layui-form-label"></label>
			    <div class="layui-input-block">
					<button class="layui-btn layui-btn-primary level-btn {if $info.recommend_num > 0}border-color{/if}">直推人数<span class="iconfont iconwenhao1" title="直接推荐的下级人数"></span><input type="hidden" value="1" /></button>
					<button class="layui-btn layui-btn-primary level-btn {if $info.order_money > 0}border-color{/if}">自购订单总额<span class="iconfont iconwenhao1" title="链动商自己购买的订单总额"></span><input type="hidden" value="2" /></button>
					<button class="layui-btn layui-btn-primary level-btn {if $info.recharge_money > 0}border-color{/if}">充值金额<span class="iconfont iconwenhao1" title="会员累计充值金额"></span><input type="hidden" value="2" /></button>
					<button class="layui-btn layui-btn-primary level-btn {if $info.up_goods_id != ''}border-color{/if}">购买指定商品<span class="iconfont iconwenhao1" title="购买特定商品"></span><input type="hidden" value="3" /></button>
			    </div>
			</div>
		</div>
	</div>
		
	<div class="layui-card card-common card-brief">
		<div class="layui-card-header">
			<span class="card-title">升级条件限制</span>
		</div>
		<div class="layui-card-body level-term">
			
            <div class="layui-form-item {if $info.recommend_num <= 0}layui-hide{/if}">
                <label class="layui-form-label" for="recommend_num"><span class="required">*</span>直推人数：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" name="recommend_num" id="recommend_num" value="{$info.recommend_num}" {if $info.recommend_num > 0}lay-verify="required|num"{/if} autocomplete="off" class="layui-input len-short" placeholder="请输入直推人数" title="直推人数">
				</div>
				<div class="layui-form-mid">人</div>
				<a href="javascript:;" class="text-color layui-form-mid" onclick="delDiv(this)">删除</a>
            </div>
			
            <div class="layui-form-item {if $info.order_money <= 0}layui-hide{/if}">
                <label class="layui-form-label" for="order_money"><span class="required">*</span>自购订单总额：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" id="order_money" name="order_money" value="{$info.order_money}" {if $info.order_money > 0}lay-verify="required|money"{/if} autocomplete="off" class="layui-input len-short" placeholder="请输入自购订单总额" title="自购订单总额">
				</div>
				<div class="layui-form-mid">元</div>
				<a href="javascript:;" class="text-color layui-form-mid" onclick="delDiv(this)">删除</a>
            </div>

			<div class="layui-form-item {if $info.recharge_money <= 0}layui-hide{/if}">
				<label class="layui-form-label" for="recharge_money"><span class="required">*</span>充值金额：</label>
				<div class="layui-input-inline">
					<input type="number" min="0" id="recharge_money" name="recharge_money" value="{$info.recharge_money}" {if $info.recharge_money > 0}lay-verify="required|money"{/if} autocomplete="off" class="layui-input len-short" placeholder="请输入充值金额" title="充值金额">
				</div>
				<div class="layui-form-mid">元</div>
				<a href="javascript:;" class="text-color layui-form-mid" onclick="delDiv(this)">删除</a>
			</div>

			<div class="layui-form-item {if $info.up_goods_id == ''}layui-hide{/if}">
				<label class="layui-form-label"><span class="required">*</span>购买指定商品：</label>
				<!-- <div class="layui-input-inline">
					<input type="text" name="up_goods_id" value="{$info.up_goods_id}" {if $info.up_goods_id != ''}lay-verify="required"{/if} autocomplete="off" class="layui-input len-short" placeholder="请输入商品ID" title="商品ID">
				</div>
				<div class="layui-form-mid">数量</div> -->
				<div class="layui-input-inline" style="width: 80px;">
					<input type="number" min="1" name="up_goods_num" value="{$info.up_goods_num}" autocomplete="off" class="layui-input" placeholder="购买数量" title="购买数量">
				</div>
				<a href="javascript:;" class="text-color layui-form-mid" onclick="delDiv(this)">删除</a>
			</div>
			
			<div class="form-row">
				<input type="hidden" name="level_id" value="{$info.level_id}">
				<button class="layui-btn" lay-submit lay-filter="save">保存</button>
				<button class="layui-btn layui-btn-primary" onclick="backLianLevelList()">返回</button>
			</div>
        </div>
    </div>
    {else/}
    <div class="form-row">
		<input type="hidden" name="level_id" value="{$info.level_id}">
		<button class="layui-btn" lay-submit lay-filter="save">保存</button>
		<button class="layui-btn layui-btn-primary" onclick="backLianLevelList()">返回</button>
	</div>
    {/if}
</div>

<script>
	$(function() {
		$('.weight-list span').not('.disabled').eq(0).addClass("border-color");
		
		// 初始处理显示/隐藏见点奖和感恩奖
		var initialLevelWeight = parseInt($('.weight-list span.border-color').attr('value')) || 1;
		if (initialLevelWeight >= 2) {
			$('.seen-point-reward').show();
			$('.peer-level-reward').show();
			$('.gratitude-reward').show();
			$('.fusee-reward').show();
		}
	});

    layui.use(['form'], function() {
        var form = layui.form,
            repeat_flag = false; //防重复标识
		form.render();
		
		// 将PHP变量转换为JS变量
		var info = {
			is_default: "{$info.is_default}"
		};
		
		$(".weight-list span").click(function() {
			if (!$(this).hasClass("disabled")) {
				$(this).addClass("border-color");
				$(this).siblings().removeClass("border-color");
				
				// 获取当前选中的等级权重
				var levelWeight = parseInt($(this).attr('value'));
				
				// 控制见点奖和感恩奖的显示
				if (levelWeight >= 2) {
					$('.seen-point-reward').show();
					$('.peer-level-reward').show();
					$('.fusee-reward').show();
				} else {
					$('.seen-point-reward').hide();
					$('.peer-level-reward').hide();
					$('.fusee-reward').hide();
					$('input[name="see_rate"], input[name="see_money"]').val('');
					$('input[name="equal_rate"], input[name="equal_money"]').val('');
					$('input[name="fusee_rate"], input[name="fusee_money"]').val('');
				}
				
				if (levelWeight >= 2) {
					$('.gratitude-reward').show();
				} else {
					$('.gratitude-reward').hide();
					$('input[name="cultivate_rate"], input[name="cultivate_money"]').val('');
				}
			}
		});

		$(".level-btn").click(function() {
			var _index = $(this).index();
			
			if (!$(this).hasClass("border-color")) {
				$(this).addClass("border-color");
				$(".level-term>div").eq(_index).removeClass("layui-hide");
				if ($(this).find("input").val() == 1) {
					$(".level-term>div").eq(_index).find("input").attr("lay-verify", "required|num");
				} else {
					$(".level-term>div").eq(_index).find("input").attr("lay-verify", "required|money");
				}
			}
		});

        form.on('submit(save)', function(data) {
			// 非默认等级才需要验证等级权重和升级条件
        	if (info.is_default == 0) {
				data.field.level_num = $(".weight-list span.border-color").attr('value');
				// 获取升级条件卡片的索引
				var arr = $(".layui-card-body.level-term").children('.layui-form-item').length;
				var arr1 = $(".layui-card-body.level-term").children('.layui-form-item.layui-hide').length;
				if (data.field.level_num == undefined){
					layer.msg('请选择等级权重');
					return;
				}	
				if(arr==arr1){
					layer.msg('请选择升级条件');
					return;
				}
			}
			
            if (repeat_flag) return;
            repeat_flag = true;

            $.ajax({
                type: 'POST',
                dataType: 'JSON',
                url: ns.url("lian://shop/level/edit"),
                data: data.field,
                success: function(res) {
                    repeat_flag = false;

                    if (res.code == 0) {
                        layer.confirm('编辑成功', {
                            title:'操作提示',
                            btn: ['返回列表', '继续编辑'],
                            yes: function(index, layero) {
                                location.hash = ns.hash("lian://shop/level/lists");
								layer.close(index);
                            },
                            btn2: function(index, layero) {
								layer.close(index);
                            }
                        });
                    }else{
                        layer.msg(res.message);
                    }
                }
            });
        });
		
		/**
		 * 表单验证
		 */
		form.verify({
			num: function(value, item) {
				var str = $(item).parents(".layui-form-item").find("label").text().split("*").join("");
				str = str.substring(0, str.length - 1);
				
				if (value <= 0) {
					return str + '必须大于0';
				}
				if (value % 1 != 0) {
					return str + '必须为整数';
				}
			},
			money: function(value, item) {
				var str = $(item).parents(".layui-form-item").find("label").text().split("*").join("");
				str = str.substring(0, str.length - 1);
				
				if (value < 0) {
					return str + '不能小于0';
				}
				var arrMen = value.split(".");
				var val = 0;
				if (arrMen.length == 2) {
					val = arrMen[1];
				}
				if (val.length > 2) {
					return str + "最多可保留两位小数";
				}
			}
		});
    });
	
	function delDiv(e) {
		var _len = $(e).parents(".layui-form-item").index();
		$(e).parents(".layui-form-item").addClass("layui-hide");
		$(e).parents(".layui-form-item").find("input").removeAttr("lay-verify");
		$(e).parents(".layui-form-item").find("input").val("");
		$(".level-btn").eq(_len).removeClass("border-color");
	}
	
	function backLianLevelList() {
		location.hash = ns.hash("lian://shop/level/lists");
	}
</script>
